package com.prs.crm.dao.oa.impl;


import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.ReferScheduleDao;
import com.prs.crm.domain.hr.Person;

import com.prs.crm.domain.oa.ReferSchedule;

public class ReferScheduleDaoImpl extends BaseDaoImpl<ReferSchedule> implements ReferScheduleDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<ReferSchedule> getAuditSchedule(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<ReferSchedule>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(ReferSchedule.class);
				criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
				criteria.createAlias("schedule", "s");
				criteria.add(Restrictions.eq("s.allotPerson", person));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<ReferSchedule> getAuditScheduleByLevel(final Person person,
			final String level) {
		// TODO Auto-generated method stub
		return (Collection<ReferSchedule>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(ReferSchedule.class);
				criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
				criteria.createAlias("schedule", "s");
				criteria.add(Restrictions.eq("s.allotPerson", person));
				criteria.add(Restrictions.eq("s.level", level));
				return criteria.list();
			}
			
		});
	}


}
